package com.zjhd.sealcutting.service.impl;

import com.zjhd.sealcutting.dao.MemberSessionidMapper;
import com.zjhd.sealcutting.dao.UserInfoMapper;
import com.zjhd.sealcutting.domain.MemberSessionid;
import com.zjhd.sealcutting.domain.UserInfo;
import com.zjhd.sealcutting.dto.SmsInfo;
import com.zjhd.sealcutting.param.LoginParam;
import com.zjhd.sealcutting.service.LoginService;
import com.zjhd.sealcutting.service.SmsService;
import com.zjhd.sealcutting.utils.CookieUtil;
import com.zjhd.sealcutting.utils.TokenUtil;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultCode;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * TODO  用户登录
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/23 10:18
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private SmsService smsService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MemberSessionidMapper memberSessionidMapper;

    /**
     * TODO  用户登录
     * @author wangchao
     * @date 2020/11/23 10:30
     * @param
     * @return
     */
    @Override
    public ResponseResult signIn(HttpServletRequest request, HttpServletResponse response, LoginParam param) throws Exception {
        if(StringUtils.isNotBlank(param.getMobile())){
            //验证登录用户手机号是否存在
            UserInfo userInfo=userInfoMapper.queryByMobile(param.getMobile());
            if(userInfo!=null){
                if(userInfo.getState().equals(0)) {
                    //判断登录验证码
                    if (StringUtils.isNotBlank(param.getSmsVerificationCode())) {
                        //判断填写验证码是否正确
                        String  smsVerificationCode = stringRedisTemplate.opsForValue().get(param.getMobile() + "-" + param.getSmsVerificationCode());
                        if (StringUtils.isNotBlank(smsVerificationCode) && smsVerificationCode.equals(param.getSmsVerificationCode())) {
                            userInfo.setToken(TokenUtil.createJwtToken(userInfo.getMobile(),userInfo));
                            CookieUtil.set(response, "Authorization", userInfo.getToken(), 14520);
                            //记录当前用户登录sessionID
                            MemberSessionid model=new MemberSessionid();
                            model.setMsId(userInfo.getId());
                            model.setMsSessionid(request.getSession().getId());
                            //查询当前用户sessionId记录是否存在
                            String session=memberSessionidMapper.queryByMsId(userInfo.getId());
                            if(StringUtils.isNotBlank(session)){
                                //存在修改
                                memberSessionidMapper.updateByPrimaryKey(model);
                            }else {
                                //不存在新增
                                memberSessionidMapper.insert(model);
                            }
                            return ResultGenerator.genSuccessResult(userInfo);
                        } else {
                            return ResultGenerator.genFailResult("登录手机验证码错误!");
                        }
                    } else {
                        return ResultGenerator.genFailResult("登录手机验证码不能为空!");
                    }
                }else {
                    return ResultGenerator.genFailResult("当前登录用户已禁用,请联系管理员!");
                }
            }else {
                return ResultGenerator.genFailResult("当前登录用户手机号不存在!");
            }
        }else {
          return ResultGenerator.genFailResult("手机号不能为空!");
        }
    }

    /**
     * TODO  验证用户登录手机号是否存
     * @author wangchao
     * @date 2020/11/23 11:21
     * @param
     * @return
     */
    @Override
    public boolean checkMobile(String mobile) throws Exception {
        boolean bool=false;
        //验证登录用户手机号是否存在
        UserInfo userInfo=userInfoMapper.queryByMobile(mobile);
        if(userInfo!=null){
            bool=true;
        }
        return bool;
    }

    /**
     * TODO  根据手机号发送短信验证码
     * @author wangchao
     * @date 2020/11/23 14:03
     * @param
     * @return
     */
    @Override
    public ResponseResult sendMobileSmsVerificationCode(LoginParam param) throws Exception {
        if(StringUtils.isNotBlank(param.getMobile())){
            //生成验证码
            int verificationCode = (int) ((Math.random() * 9 + 1) * 100000);
            SmsInfo smsInfo = new SmsInfo();
            smsInfo.setMobile("+86" + param.getMobile());
            smsInfo.setTemplateId("686b7cc2cfbb4d0394cdbf1f2fcbf00a");
            smsInfo.setTemplateParas("[" + verificationCode + "]");
            stringRedisTemplate.opsForValue().set(param.getMobile()+"-"+verificationCode,String.valueOf(verificationCode), 5, TimeUnit.MINUTES);
            smsService.sendSms(smsInfo);
            return ResultGenerator.genSuccessResult("发送成功！");
        }else {
            return ResultGenerator.genFailResult("手机号不能为空!");
        }
    }


}
